package com.cms.service;

import java.util.List;
import com.cms.model.AppChannelApkInfo;
import com.cms.model.AppInfo;
import com.cms.model.AppVersionInfo;
import com.model.Admin;
import com.service.AbstractDateService;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class AppChannelApkInfoServiceImpl extends AbstractDateService implements
AppChannelApkInfoService {
	
	@Override
	public boolean deleteAppChannelApkInfo(AppChannelApkInfo model) {
		String sql = "update t_appchannelapkinfo set isdel='1' where id='"+model.getId()+"'";
		logger.info("[SQL] [method:deleteAppChannelApkInfo] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public AppChannelApkInfo getAppChannelApkInfoById(int id) {
		String sql = "select * from t_appchannelapkinfo where id='"+id+"' order by createdate desc";
		logger.info("[SQL] [method:getAppChannelApkInfoById] ["+sql+"]");
		ModelGenerator<AppChannelApkInfo> model = new ModelGenerator<AppChannelApkInfo>(AppChannelApkInfo.class);
		return model.generate(base,sql.toString());
	}

	@Override
	public AppChannelApkInfo getAppChannelApkInfoByChannelApkNumber(String channelapknumber) {
		String sql = "select * from t_appchannelapkinfo where channelapknumber='"+channelapknumber+"' order by createdate desc";
		logger.info("[SQL] [method:getAppChannelApkInfoById] ["+sql+"]");
		ModelGenerator<AppChannelApkInfo> model = new ModelGenerator<AppChannelApkInfo>(AppChannelApkInfo.class);
		return model.generate(base,sql.toString());
	}

	@Override
	public List<AppChannelApkInfo> getAppChannelApkInfos() {
		String sql = "select * from t_appchannelapkinfo where isdel='0' order by createdate desc";
		logger.info("[SQL] [method:getAppChannelApkInfos] ["+sql+"]");
		ModelGenerator<AppChannelApkInfo> model = new ModelGenerator<AppChannelApkInfo>(AppChannelApkInfo.class);
		return model.generateList(base,sql.toString());
	}

	@Override
	public boolean saveAppChannelApkInfo(AppChannelApkInfo model) {
		model.setId(getTableId("t_appchannelapkinfo"));
		String sql = "insert into t_appchannelapkinfo (id,channelapknumber,appnumber" +
				",appversionid,appversionnumber,appname,cpid,categoryparentid,categoryid,channelid,projectid"+
		",apppackage,appstatus,appversion,appmd5,appiconname,reiconname,appiconsrc,appfilename"+
		",refilename,appfilesrc,appentry,createuser,createdate,appremark,channelapkid) " +
		"values ('"+model.getId()+"','"+model.getChannelapknumber()+"','"
		+model.getAppnumber()+"','"+model.getAppversionid()+"','"+model.getAppversionnumber()+"','"+model.getAppname()+"','"
		+model.getCpid()+"','"+model.getCategoryparentid()+"','"+model.getCategoryid()+
		"','"+model.getChannelid()+"','"+model.getProjectid()+"','"+model.getApppackage()+"','"+model.getAppstatus()+"','"+model.getAppversion()+"','"+model.getAppmd5()+"','"+model.getAppiconname()+
		"','"+model.getReiconname()+"','"+model.getAppiconsrc()+"','"+model.getAppfilename()+"','"+model.getRefilename()+"','"+model.getAppfilesrc()+
		"','"+model.getAppentry()+"','"+model.getCreateuser()+"','"+model.getCreatedate()+"','"+model.getAppremark()+"','"+model.getChannelapkid()+"')";
		logger.info("[SQL] [method:saveAppChannelApkInfo] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public boolean saveAppChannelApkInfoStatus(AppChannelApkInfo model) {
		String sql = "update t_appchannelapkinfo set appstatus='" + model.getAppstatus()+ "' " +
		" where id=" + model.getId();
		logger.info("[SQL] [method:saveAppChannelApkInfoStatus] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public boolean updateAppChannelApkInfo(AppChannelApkInfo model) {
		String sql = "update t_appchannelapkinfo set appname='" + model.getAppname() + "'," +
		"apppackage='" + model.getApppackage() + "', " +
		"appversion='" + model.getAppversion() + "' " +
		",appversionid=" + model.getAppversionid() + 
		",appversionnumber='" + model.getAppversionnumber() + "'"+
		",appmd5='" + model.getAppmd5()+ "' " +
		",appiconname='" + model.getAppiconname() + "' " +
		",reiconname='" + model.getReiconname() + "' " +
		",appiconsrc='" + model.getAppiconsrc() + "' " +
		",appfilename='" + model.getAppfilename() + "' " +
		",refilename='" + model.getRefilename() + "' " +
		",appfilesrc='" + model.getAppfilesrc() + "' " +
		",appentry='" + model.getAppentry() + "' " +
		",appremark='" + model.getAppremark() + "' " +
		",channelid='" + model.getChannelid() + "' " +
		",projectid='" + model.getProjectid() + "' " +
		",channelapkid='" + model.getChannelapkid() + "' " +
		" where id='" + model.getId() + "'";
		logger.info("[SQL] [method:updateAppChannelApkInfo] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public boolean updateAppChanelApkFile(AppChannelApkInfo model) {
		String sql = "update t_appchannelapkinfo set appfilename='" + model.getAppfilename() + "' " +
				",refilename='" + model.getRefilename() + "' " +
				",appfilesrc='" + model.getAppfilesrc() + "' " +
				" where channelapknumber='" + model.getChannelapknumber() + "'";
				logger.info("[SQL] [method:updateAppChanelApkFile] ["+sql+"]");
				return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public boolean changescale(AppChannelApkInfo model) {
		String sql="update t_appchannelapkinfo set scale="+model.getScale()+" where id="+model.getId();
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public List<AppChannelApkInfo> getAppChannelApkInfos(String appnumber,
			 int appversionid,
			int channelid, int projectid, String appname) {
		String sql = "select * from t_appchannelapkinfo where 1=1 and isdel='0' ";
		
		if(appnumber!=null && appnumber.length()>0)
			sql+=" and appnumber='"+appnumber+"' ";
		if(appversionid>0)
			sql+=" and appversionid='"+appversionid+"' ";
		if(channelid>0)
			sql+=" and channelid='"+channelid+"' ";
		if(projectid>0)
			sql+=" and projectid='"+projectid+"' ";
		if(appname!=null && appname.length()>0)
			sql+=" and appname like '%"+appname+"%' ";
		sql+=" order by createdate desc";
		logger.info("[SQL] [method:getAppChannelApkInfos] ["+sql+"]");
		ModelGenerator<AppChannelApkInfo> model = new ModelGenerator<AppChannelApkInfo>(AppChannelApkInfo.class);
		return model.generateList(base,sql);
	}

	@Override
	public List<AppChannelApkInfo> getAppChannelApkByAppnumber(String appnumber) {
		String sql = "select * from t_appchannelapkinfo where appnumber='"+appnumber+"'  order by createdate desc";
		logger.info("[SQL] [method:getAppChannelApkByAppnumber] ["+sql+"]");
		ModelGenerator<AppChannelApkInfo> model = new ModelGenerator<AppChannelApkInfo>(AppChannelApkInfo.class);
		return model.generateList(base,sql.toString());
	}

	@Override
	public List<AppChannelApkInfo> getAppChannelApksByAdmin(Admin admin) {
		String sql="select * from t_appchannelapkinfo where channelid in (select channelid from t_adminchannel where adminid="+admin.getId()+")";
		ModelGenerator<AppChannelApkInfo> model = new ModelGenerator<AppChannelApkInfo>(AppChannelApkInfo.class);
		return model.generateList(base,sql.toString());
	}

}
